﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Ammo.cs" company="">
//   
// </copyright>
// <summary>
//   The ammo.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RescueRaiders.Models
{
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    using RescueRaiders.Properties;

    /// <summary>
    /// The ammo.
    /// </summary>
    internal sealed class Ammo : BaseModel
    {
        /// <summary>
        /// The base velocity.
        /// </summary>
        private const float baseVelocity = 10f;

        /// <summary>
        /// The max frames.
        /// </summary>
        private const int maxFrames = 3;

        /// <summary>
        /// The max range.
        /// </summary>
        private readonly float maxRange;

        /// <summary>
        /// The current frame.
        /// </summary>
        private int currentFrame;

        /// <summary>
        /// The velocity.
        /// </summary>
        private Vector2 velocity;

        /// <summary>
        /// Initializes a new instance of the <see cref="Ammo"/> class.
        /// </summary>
        /// <param name="texture">
        /// The texture.
        /// </param>
        /// <param name="maxRange">
        /// The max range.
        /// </param>
        public Ammo(Texture2D texture, float maxRange)
            : base(texture, Vector2.Zero, Side.None)
        {
            frameSize = new Point(texture.Width / maxFrames, texture.Height);
            this.maxRange = maxRange;
            Center = new Vector2(frameSize.X / 2f, frameSize.Y / 2f);
        }

        /// <summary>
        /// Gets RangeLeft.
        /// </summary>
        public float RangeLeft { get; private set; }

        /// <summary>
        /// Gets SourceRectangle.
        /// </summary>
        protected override Rectangle? SourceRectangle
        {
            get
            {
                return new Rectangle(currentFrame * frameSize.X, 0, frameSize.X, frameSize.Y);
            }
        }

        /// <summary>
        /// The fire.
        /// </summary>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <param name="direction">
        /// The direction.
        /// </param>
        /// <param name="rotation">
        /// The rotation.
        /// </param>
        /// <param name="soundEmitter">
        /// The sound emitter.
        /// </param>
        public void Fire(Vector2 position, Vector2 direction, float rotation, Vector3 soundEmitter)
        {
            Texturedata = new Color[(frameSize.X * frameSize.Y)];
            Texture.GetData(0, SourceRectangle, Texturedata, 0, Texturedata.Length);
            RangeLeft = maxRange;
            Emitter.Position = -soundEmitter;
            var cue = Game1.SoundBank.GetCue(Settings.Default.AmmoFireCue);
            cue.Apply3D(Game1.Listener, Emitter);
            cue.Play();
            Position = position;
            Rotation = rotation;
            velocity = baseVelocity * direction;
        }

        /// <summary>
        /// The update.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public override void Update(GameTime gameTime)
        {
            RangeLeft -= velocity.Length();
            Position += velocity;
            currentFrame = ++currentFrame % maxFrames;
        }
    }
}